﻿<%@ WebHandler Language="C#" Class="WXActivity.ActivityMQHandler" %>

using System;
using System.Globalization;
using Activity;
using Activity.API;
namespace WXActivity
{
    /// <summary>
    /// 抽奖活动处理程序
    /// </summary>
    public class ActivityMQHandler : BaseHandler
    {

        public ActivityMQHandler()
        {
            //修改请求为post 方式
            base._httpReuqest = base.Context.Request.Form;
        }

        /// <summary>
        /// 校验用户，成功返回积分
        /// </summary>
        public void InitUser()
        {
            var date = DateTime.Now;
            var code = _httpReuqest["code"];
            if (string.IsNullOrWhiteSpace(code))
            {
                Error("无效的[Code]值", -1);
            }
            else
            {
                var lotteryQueueHandler = new LotteryQueueHandler();
                Log.WriteLog("请求openId开始："+date.ToString(CultureInfo.InvariantCulture));
                var user = lotteryQueueHandler.GetOauth2AccessToken(code);
                Log.WriteLog("请求openId结束："+date.ToString(CultureInfo.InvariantCulture));
                if (user == null || user.openid == null)
                {
                    Error("用户不存在", -2);
                }
                else
                {
                    Log.WriteLog("请求用户信息开始："+date.ToString(CultureInfo.InvariantCulture));
                    var result = lotteryQueueHandler.GetUser(user.openid);
                    Log.WriteLog("请求用户信息结束："+date.ToString(CultureInfo.InvariantCulture));
                    if (result != null)
                        Success(result);
                    else
                        Error("请先注册会员", -3);
                }
            }
        }

        /// <summary>
        /// 返回用户积分
        /// </summary>
        public void GetUserIntegral()
        {
            var openId = _httpReuqest["openId"];
            var lotteryQueueHandler = new LotteryQueueHandler();
            var result = lotteryQueueHandler.GetUser(openId);
            if (result != null)
                Success(result);
            else
                Error("会员不存在", -3);
        }

        /// <summary>
        /// 51,夏季积分抽奖
        /// </summary>
        public void LotteryMQIntegral()
        {
            var type = _httpReuqest["type"];
            var openId = _httpReuqest["openId"];
            if (!string.IsNullOrWhiteSpace(type))
            {
                var handler = new LotteryQueueHandler();
                Success(handler.Lottery(Convert.ToInt16(type), openId));
            }
            else
                Error("类型错误");
        }

        /// <summary>
        /// 中奖列表
        /// </summary>
        public void List()
        {
            var type = _httpReuqest["type"];
            var openId = _httpReuqest["openId"];
            if (!string.IsNullOrWhiteSpace(type))
            {
                var handler = new LotteryQueueHandler();
                Success(handler.List(openId, Convert.ToInt16(type)));
            }
            else
                Error("类型错误");
        }
    }
}